﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Donate.DAl;

namespace Donate
{
    public partial class Donors : Form
    {
        public Donors()
        {
            InitializeComponent();
        }
        private Int32 currentID = -1;
        private Int32 historyID = -1;        
        private Int32 CurrentDonater = -1;

        //***********************
        //Hiostry
        public DonHistoryEntity getHistoryFromForm()
        {
            DonHistoryEntity d = new DonHistoryEntity();
            d.DonTo = txtdonto.Text;
            d.Notes = txtnot.Text;
            d.DateOfDonate = pkrdondate.Value.Date;
            if (!String.IsNullOrEmpty(txtId.Text))
            {
                d.DonId = int.Parse(txtId.Text);
            }
            else
            {
                MessageBox.Show("من فضلك أدخل بيانات المتبرع");
            }
            return d;
        }

        public void publishGistoyToForm(DonHistoryEntity d)
        {
            txtdonto.Text = d.DonTo ;
             txtnot.Text =d.Notes ;
             pkrdondate.Value =d.DateOfDonate ;
             txtId.Text =d.DonId.ToString();
             txtdonateGet.Text = d.Id.ToString();
             historyID = int.Parse(txtId.Text);
        }
        public void clearHistory()
        {
            txtdonto.Text = "";
            txtnot.Text = "";
            pkrdondate.Value = DateTime.Now;
            txtdonateGet.Text = "";
            currentID = -1;

        }
        public void publishHistoryByName()
        {
            dataGridHistory.DataSource = DonHistory.GetHistoryByName( txtdonate.Text );
        }

        //*******************************
        //Donor
        public DonatesEntity GetEntityFromForm()
        {
            DonatesEntity d = new DonatesEntity();
            d.Nameof = txtdonate.Text;
            d.Mobile = txtmobile.Text;
            d.Mobile2 = txtmobile2.Text;
            d.Phone = txtphone.Text;
            d.Type = cmbotype.Text;
            d.Supervivor = txtsupervivor.Text;
            d.Dob = pkrdob.Value.Date;
            d.LastDonDate = pkrlastdonate.Value.Date;
            d.Notes = txtnotes.Text;
            // d.CanDonate =Convert.ToBoolean( rdioyes.Text);
            //d.CanDonate = Convert.ToBoolean(rdiono.Text);
            if (rdiomale.Checked == true)
                d.Gender = "ذكر";
            else
                d.Gender = "أنثي";
            if (rdioyes.Checked == true)
                d.CanDonate = ("نعم");
            else
                d.CanDonate = ("لا");

            return d;
        }
        public void PublishDonatesToForm(DonatesEntity d)
        {
            txtId.Text = d.Id.ToString();
            txtdonate.Text = d.Nameof;
            txtsupervivor.Text = d.Supervivor;
            txtmobile.Text = d.Mobile;
            txtmobile2.Text = d.Mobile2;
            txtphone.Text = d.Phone;
            txtnotes.Text = d.Notes;
            cmbotype.Text = d.Type;
            pkrdob.Value = d.Dob;
            pkrlastdonate.Value = d.LastDonDate;
            currentID = Int32.Parse(txtId.Text);
            if (GlobalUtils.GENDER_MALE == d.Gender)
            {
                rdiomale.Checked = true;
            }
            else if (GlobalUtils.GENDER_FEMALE == d.Gender)
            {
                rdiofemale.Checked = true;
            }
            if (GlobalUtils.Can_Donate_yes == Convert.ToString(d.CanDonate))
            {
                rdioyes.Checked = true;

            }
            else if (GlobalUtils.Can_Donate_No == Convert.ToString(d.CanDonate))
            {
                rdiono.Checked = true;
            }

        }
        private void CleanAllFields()
        {
            currentID = -1;
            txtdonate.Text = "";
            txtsupervivor.Text = "";
            txtmobile.Text = "";
            txtmobile2.Text = "";
            txtphone.Text = "";
            txtnotes.Text = "";
            cmbotype.Text = "";
            pkrdob.Value = DateTime.Now;
            pkrlastdonate.Value = DateTime.Now;
            rdiofemale.Checked = false;
            rdiomale.Checked = false;
            rdiono.Checked = false;
            rdioyes.Checked = false;


        }
        private void btnclear_Click(object sender, EventArgs e)
        {
            CleanAllFields();
            clearHistory();
            List<DonHistoryEntity> list = new List<DonHistoryEntity>();
            dataGridHistory.DataSource = list;
        }
        private void btnsave_Click(object sender, EventArgs e)
        {
            if (currentID == -1)
            {
                if (!String.IsNullOrEmpty(txtdonate.Text))
                {
                    DonatesEntity Donate = GetEntityFromForm();
                    if (Donates.Insert(Donate))
                    {
                        MessageBox.Show("تم الحفظ");
                        CleanAllFields();
                        //currentID = Int32.Parse(txtId.Text);
                    }
                    else
                    {
                        MessageBox.Show("خطأ لم يتم الحفظ");
                    }
                }
                else
                {
                    MessageBox.Show("من فضلك أدخل بيانات المتبرع");
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(txtdonate.Text))
                {
                    DonatesEntity Donate = GetEntityFromForm();
                    Donate.Id = Int32.Parse(txtId.Text);
                    if (Donates.Update(Donate))
                    {
                        MessageBox.Show("تم التعديل");
                        currentID = Int32.Parse(txtId.Text);
                    }
                    else
                    {
                        MessageBox.Show("خطأ لم يتم التعديل");
                    }
                }
                else
                {
                    MessageBox.Show("من فضلك أدخل بيانات المتبرع");
                }
            }
        }
        private void btndelete_Click(object sender, EventArgs e)
        {

            if (CurrentDonater != -1)
            {

                DialogResult Result = MessageBox.Show(" هل تريد حذف" +  txtdonate.Text + "?", "confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (Result == System.Windows.Forms.DialogResult.Yes)
                {
                    if (Donates.Delete(CurrentDonater))
                    {
                        MessageBox.Show("تم حذف العميل بنجاح");
                        CurrentDonater = -1;
                    }
                    else
                    {
                        MessageBox.Show("حدث خطأ عند الحذف");
                    }

                }
                else
                {
                    MessageBox.Show("بعض البيانات غير موجوده");
                }
                CleanAllFields();

            }
        }
        public void PublishDataFromGrid(int Row)
        {
            txtId.Text = dataGridView2.Rows[Row].Cells["cId"].Value.ToString();
            txtdonate.Text = dataGridView2.Rows[Row].Cells["clmname"].Value.ToString();
            pkrdob.Value = DateTime.Parse(dataGridView2.Rows[Row].Cells["clmdob"].Value.ToString());
            cmbotype.Text = dataGridView2.Rows[Row].Cells["clmType"].Value.ToString();
            txtmobile.Text = dataGridView2.Rows[Row].Cells["clmMobile"].Value.ToString();
            txtmobile2.Text = dataGridView2.Rows[Row].Cells["clmMobile2"].Value.ToString();
            txtphone.Text = dataGridView2.Rows[Row].Cells["clmPhone"].Value.ToString();
            pkrlastdonate.Value = DateTime.Parse(dataGridView2.Rows[Row].Cells["clmLastDonDate"].Value.ToString());
            if (dataGridView2.Rows[Row].Cells["clmCanDonate"].Value.ToString() == "نعم")
            {
                rdioyes.Checked = true;
            }
            else 
            {
                rdiono.Checked = true;
            }
            if (dataGridView2.Rows[Row].Cells["clmGender"].Value.ToString() == "ذكر")
            {
                rdiomale.Checked = true;
            }
            else 
            {
                rdiofemale.Checked = true;
            }
            txtnotes.Text = dataGridView2.Rows[Row].Cells["clmNotes"].Value.ToString();
            txtsupervivor.Text = dataGridView2.Rows[Row].Cells["clmSupervivor"].Value.ToString();

            string x = dataGridView2.Rows[Row].Cells["clmname"].Value.ToString();

            //CurrentDonater = Convert.ToInt32(x);

        }
        private void Showgridview(List<DonatesEntity> d)
        {
            dataGridView2.DataSource = d;
            dataGridView2.Visible = true;
        }
        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            PublishDataFromGrid(e.RowIndex);
            publishHistoryByName();
            dataGridView2.Visible = false;
        }
        private void txtdonate_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyValue == 13)
            {
                List<DonatesEntity> d = Donates.GetByName("%"+txtdonate.Text+"%");
                if (d.Count == 0)
                {
                    MessageBox.Show("هذا العميل غير موجود");
                }
                else if (d.Count == 1)
                {
                    PublishDonatesToForm(d[0]);
                    publishHistoryByName();
                }
                else
                    Showgridview(d);
 
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (historyID == -1)
            {
                if (!String.IsNullOrEmpty(txtdonate.Text))
                {
                    if (!String.IsNullOrEmpty(txtdonto.Text))
                    {
                        if (DonHistory.Add(getHistoryFromForm()))
                        {
                            MessageBox.Show("تم الحفظ بنجاح");
                          //  dataGridHistory.DataSource = DonHistory.GetAllByDonId();
                            publishHistoryByName();
                            clearHistory();
                        }
                    }
                    else
                    {
                        MessageBox.Show("من فضلك أدخل إسم المتبرع له");
                    }
                }
                else
                {
                    MessageBox.Show("من فضلك أدخل بيانات المتبرع");
                }
                
            }
            else
            {
                if (!String.IsNullOrEmpty(txtdonate.Text))
                {
                    if (!String.IsNullOrEmpty(txtdonto.Text))
                    {
                        DonHistoryEntity don = new DonHistoryEntity();
                        don.Id = Int32.Parse(txtdonateGet.Text);
                        if (DonHistory.Update(getHistoryFromForm()))
                        {
                            MessageBox.Show("تم التعديل بنجاح");
                            dataGridHistory.DataSource = DonHistory.GetAllByDonId();
                            clearHistory();
                        }
                    }
                    else
                    {
                        MessageBox.Show("من فضلك أدخل إسم المتبرع له");
                    }
                }
                else
                {
                    MessageBox.Show("من فضلك أدخل بيانات المتبرع");
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtdonateGet.Text))
            {
                DialogResult result = MessageBox.Show("هل تريد حذف بيانات المتبرع له", "", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    if (DonHistory.Delete(Int32.Parse((txtdonateGet.Text))))
                    {
                        MessageBox.Show("تم الحذف");
                    }
                    else
                        MessageBox.Show("خطأفى الحذف");
                }
            }
            else
                MessageBox.Show("من فضلك إدخل بيانات المتبرع له");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            clearHistory();
        }

        private void dataGridHistory_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            publishToHistoryFromGrid(e.RowIndex);
        }
        private void publishHistory(DonHistoryEntity h)
        { 
        txtdonto.Text=h.DonTo;
            pkrdondate.Value= h.DateOfDonate;
            txtnot.Text = h.Notes;

        }
        private void label8_Click(object sender, EventArgs e)
        {

        }
        public void publishToHistoryFromGrid(Int32 id)
        {
            txtdonto.Text = dataGridHistory.Rows[id].Cells["clmMobile2"].Value.ToString();
            txtnot.Text = dataGridHistory.Rows[id].Cells["clmPhone"].Value.ToString();
            pkrdondate.Value = DateTime.Parse(dataGridHistory.Rows[id].Cells["clmLastDonDate"].Value.ToString());
        }



    }
}
